import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o visible = false;
    @o isUpdate = false;
    @o isSecond = false;
    @o agentId = '';//代理商id  列表的id
    @o agentPhone = '';//手机号
    @o agentCountryCode = ''; //国家代码
    @o agentSelectCountry = {};//国家区号  对象
    @o agentName = ''; //姓名
    @o agentGivingTime = '';//赠送时长
    @o firstLevelAgentsList = [];//一级代理商列表
    @o selectFirstAgentId = '';//选择的一级代理商
    @o selectFirstAgentName = '';//选择的一级代理商名字

    will() {

    }

    closeDialog() {
        this.visible = false;
        this.isUpdate = false;
        this.isSecond = false;
        this.agentId = '';
        this.agentPhone = '';
        this.agentCountryCode = '';
        this.agentSelectCountry = {};
        this.agentName = '';
        this.agentGivingTime = '';
        this.firstLevelAgentsList = [];
        this.selectFirstAgentId = '';
        this.selectFirstAgentName = '';
    }

    checkInput() {
        if (this.isSecond && !this.selectFirstAgentId) {
            this.stores.Message.showMessage(3, '请选择一级代理商');
            return true;
        }
        if (!this.isUpdate && (!this.agentPhone || !this.agentPhone.toString().trim())) {
            this.stores.Message.showMessage(3, '请输入手机号');
            return true;
        }
        if (!this.isUpdate && ((this.agentSelectCountry.code === $$.SEM.CN_COUNTRY_CODE &&
                !$$.utils.formRegular.phone(this.agentPhone)) ||
                (this.agentSelectCountry.code !== $$.SEM.CN_COUNTRY_CODE && this.agentPhone.length < 7))) {
            this.stores.Message.showMessage(3, '请输入正确的手机号格式');
            return true;
        }
        if (!this.isUpdate && (!this.agentName || !this.agentName.toString().trim())) {
            this.stores.Message.showMessage(3, '请输入姓名');
            return true;
        }
        if (!this.isUpdate && !$$.utils.formRegular.nickname(this.agentName)) {
            this.stores.Message.showMessage(3, '请输入正确的姓名，10个汉字或20个字符');
            return true;
        }
        if (this.isUpdate && !this.isSecond && !this.agentGivingTime) {
            this.stores.Message.showMessage(3, '请输入赠送时长');
            return true;
        }
    }

    confirmDialog() {
        if (this.checkInput()) {
            return;
        }
        this.stores.Frame.showLoading();
        let countryCode = this.agentSelectCountry.code === $$.SEM.CN_COUNTRY_CODE ?
            '' : this.agentSelectCountry.code;
        countryCode = this.isUpdate ? null : countryCode;
        request({
            url: this.isUpdate ? $$.api('agentsUpdate') : $$.api('agentsInsert'),
            method: this.isUpdate ? 'put' : 'post',
            data: {
                inviteId: this.agentId || null,
                parentId: this.selectFirstAgentId || null,
                countryCode: countryCode,
                phone: this.agentPhone || null,
                time: this.agentGivingTime || '',
                name: this.agentName || null,
            }
        }).then(res => {
            this.stores.Message.showMessage(1, res.message);
            this.stores.Frame.hideLoading();
            this.closeDialog();
            this.fire('agentsDialog:update');
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });

    }

    agentNameChange(v) {
        this.agentName = v;
    }

    selectCodeItem(item) {
        if (item.code === this.agentSelectCountry.code) {
            return;
        }
        this.agentSelectCountry = item;
        this.agentCountryCode = item.code === $$.SEM.CN_COUNTRY_CODE ? '' : item.code;
        this.agentPhone = '';
    }

    agentPhoneChange(v) {
        this.agentPhone = v;
    }

    agentGivingTimeChange(v) {
        this.agentGivingTime = v;
    }

    selectFirstLevelChange(v) {
        this.selectFirstAgentId = v;
    }


    mount() {
        this.on('agentsDialog:show', (data) => {
            this.visible = true;
            let code = $$.SEM.CN_COUNTRY_CODE;
            this.agentSelectCountry = this.stores.Frame.countryCodeList.find(i => {
                return code === i.code;
            });
            if (data) {
                this.isSecond = !!data.firstAgentId;
                this.isUpdate = !!data.id;
                let agentsList = data.agentsList || [];
                agentsList.map(item => {
                    this.firstLevelAgentsList.push({label: item.name, value: item.id});
                })
                this.selectFirstAgentId = data.firstAgentId || '';
                this.selectFirstAgentName = data.firstAgentName || '';
                this.agentId = data.id || '';
                this.agentGivingTime = data.time || '';
            }
        });
    }

}

export default Store;